<template>
  <div class="guideline-page">
    <!-- 返回按钮 -->
    <button class="back-button" @click="handleBack">
      <svg
        xmlns="http://www.w3.org/2000/svg"
        width="20"
        height="20"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        stroke-width="2"
        stroke-linecap="round"
        stroke-linejoin="round">
        <path d="M19 12H5M5 12l7 7 7-7" />
      </svg>
      <span>Back</span>
    </button>

    <div class="guideline-container">
      <!-- 标题区域 -->
      <header class="guideline-header">
        <h1>Nexbie Guideline: Welcome to the Community</h1>
        <p class="intro">
          Welcome to Nexbie, a place where your creativity is supercharged by AI. Our goal is to
          provide a space for you to unleash your imagination, share it with the world, and even
          turn your digital designs into physical products. Here are the core features of the Nexbie
          universe:
        </p>
      </header>

      <!-- 主体内容区 -->
      <div class="guideline-content">
        <!-- 左侧目录 -->
        <aside class="guideline-sidebar">
          <nav class="toc">
            <ul>
              <li
                v-for="(item, index) in guidelineList"
                :key="index"
                :class="{ active: activeIndex === index }"
                @click="scrollToSection(index)"
                class="toc-item">
                <span class="toc-number">{{ index + 1 }}.</span>
                <span class="toc-title">{{ item.title }}</span>
              </li>
            </ul>
          </nav>
        </aside>

        <!-- 右侧内容 -->
        <main class="guideline-main">
          <div class="sections-container" ref="sectionsContainer" @scroll="handleScroll">
            <section
              v-for="(item, index) in guidelineList"
              :key="index"
              :ref="(el) => (sectionRefs[index] = el)"
              class="guideline-section">
              <h2 class="section-heading">
                <span class="heading-number">{{ index + 1 }}.</span>
                {{ item.title }}
              </h2>
              <div class="section-body" v-html="item.content"></div>
            </section>
          </div>
        </main>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { ref, onMounted, nextTick } from 'vue';
  import { useRouter } from 'vue-router';
  const router = useRouter();

  // 社区指南数据（完全使用原文案）
  const guidelineList = ref([
    {
      title: 'What is Nexbie？',
      content: `
      <p>Welcome to Nexbie — where imagination meets creation.</p>
      <p style="font-weight: 500; margin: 16px 0;">Dream it with AI. Make it real. Discover endless cool finds born from creativity.</p>

      <ul>
        <li><strong>Nex Lab (AIGC Creation)</strong><br>
        At the heart of Nexbie is Nex Lab, your personal AI creation studio. Whether you're starting with a simple text prompt (text-to-image) or an existing picture (image-to-image), our powerful tools help you generate unique and stunning visuals in moments. This is where every masterpiece begins.</li>

        <li><strong>Community Posts</strong><br>
        Your profile is your gallery. Post your creations for the community to see, get feedback, and gain recognition. But it doesn't stop there. Popular original works can even get a chance to be commercialized!</li>

        <li><strong>Nexbie Arcade (Competitions)</strong><br>
        Ready to test your skills? The Nexbie Arcade is our periodic creative competition hub. Each contest comes with a unique theme, giving you a chance to challenge yourself, showcase your talent, and win real cash prizes from our prize pool.</li>

        <li><strong>Manage Your Balance</strong><br>
        Already got some profits from Nexbie? Simply send an email to contact@nexbie.com to manage your balance, including checking the balance statement and requesting a balance withdrawal</li>
      </ul>
    `,
    },
    {
      title: 'What is Nex?',
      content: `
      <p>"Nex" is our unique feature that turns community-loved art into real-world products through crowdfunding. It’s a risk-free way for your AIGC creations to make the leap from screen to reality.</p>

      <p><strong>How does it work?</strong></p>
      <ol type="1">
        <li><strong>Platform Selection:</strong> Our team keeps an eye out for the most promising designs, whether they're winning entries in the Nexbie Arcade or viral hits from Community Posts. Users cannot start a Nex for their own work.</li>

        <li><strong>Campaign Launch:</strong> When a piece is selected, we launch a "Nex" for it. We set a funding goal, a price, and a time limit for the campaign.</li>

        <li><strong>Community Backing:</strong> Anyone in the community can support the design by crowdfunding the item.</li>

        <li><strong>Success or Failure:</strong>
          <ul>
            <li><strong>Nex Success:</strong> If the funding goal is met within the time limit, the campaign is a success! The item goes into production, and backers receive the product they helped bring to life.</li>
            <li><strong>Nex Fail:</strong> If the goal isn't met, the campaign fails. Your design remains on your profile as a regular post for everyone to appreciate while all users who crowdfund the project will be refunded.</li>
          </ul>
        </li>
      </ol>

      <p>Nex empowers our community to decide what gets made, ensuring that the most desired creations become tangible products.</p>
    `,
    },
    {
      title: 'Who are Pro Users?',
      content: `
      <p>A Nexbie Pro User is a creator who has been verified by our team and granted access to an expanded set of professional-grade tools and privileges. This status is designed for artists and designers who want more control and flexibility over their work on the platform.</p>

      <p><strong>Exclusive Pro Benefits:</strong></p>
      <ul>
        <li><strong>Upload Your Own Designs:</strong> This is the biggest perk. While standard users create with our AI, Pro Users can upload their own finished designs, including high-resolution images (JPG, PNG) and even 3D engineering and modeling files (STL, 3MF). This allows you to integrate your existing workflow and portfolio with the Nexbie ecosystem.</li>

        <li><strong>Priority Consideration:</strong> Pro User creations receive priority when we select products for the official Nexbie store.</li>
      </ul>

      <p><strong>How to Become a Pro User:</strong></p>
      <p>If you are a professional creator, you can apply through our platform. The application requires you to submit your basic information and provide qualification materials, such as a link to your portfolio or professional social media pages, for review.</p>
    `,
    },
  ]);

  // 状态管理
  const activeIndex = ref(0);
  const sectionsContainer = ref<HTMLDivElement | null>(null);
  const sectionRefs = ref<any>([]);
  const scrollTimeout = ref<number | null>(null);

  // 滚动到指定章节
  const scrollToSection = (index: number) => {
    const container = sectionsContainer.value;
    const section = sectionRefs.value[index];

    if (container && section) {
      activeIndex.value = index;

      // 精确计算滚动位置
      const containerRect = container.getBoundingClientRect();
      const sectionRect = section.getBoundingClientRect();
      const scrollPosition = sectionRect.top - containerRect.top + container.scrollTop - 20;

      container.scrollTo({
        top: scrollPosition,
        behavior: 'smooth',
      });
    }
  };

  // 滚动监听处理
  const handleScroll = () => {
    if (scrollTimeout.value) {
      window.clearTimeout(scrollTimeout.value);
    }

    scrollTimeout.value = window.setTimeout(() => {
      if (!sectionsContainer.value) return;

      const container = sectionsContainer.value;
      const scrollTop = container.scrollTop;

      for (let i = 0; i < sectionRefs.value.length; i++) {
        const section = sectionRefs.value[i];
        if (!section) continue;

        const sectionTop = section.offsetTop;
        const sectionHeight = section.offsetHeight;

        if (sectionTop <= scrollTop + 50 && sectionTop + sectionHeight > scrollTop) {
          activeIndex.value = i;
          break;
        }
      }
    }, 100);
  };

  // 返回按钮处理
  const handleBack = () => {
    console.log('返回上一页');
    // 实际应用中使用路由返回
    router.back();
  };

  // 初始化
  onMounted(() => {
    nextTick(() => {
      handleScroll();
    });
  });
</script>

<style scoped>
  .guideline-page {
    min-height: 100vh;
    background-color: #f9f9f9;
    padding: 20px;
    box-sizing: border-box;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    position: relative;
  }

  /* 返回按钮 */
  .back-button {
    display: flex;
    align-items: center;
    gap: 8px;
    background: transparent;
    border: none;
    color: #f76b1b;
    font-size: 14px;
    cursor: pointer;
    padding: 8px 12px;
    border-radius: 6px;
    transition: all 0.2s ease;
    position: absolute;
    top: 20px;
    left: 20px;
    z-index: 10;
  }

  .back-button:hover {
    background-color: rgba(247, 107, 27, 0.1);
  }

  .guideline-container {
    max-width: 1400px;
    margin: 0 auto;
    padding-top: 40px;
  }

  /* 标题区域 */
  .guideline-header {
    text-align: center;
    margin-bottom: 30px;
    padding: 0 20px;
  }

  .guideline-header h1 {
    color: #f76b1b;
    font-size: 28px;
    margin-bottom: 16px;
  }

  .intro {
    color: #333;
    line-height: 1.6;
    font-size: 15px;
    max-width: 1000px;
    margin: 0 auto;
  }

  /* 主体内容区布局 */
  .guideline-content {
    display: flex;
    gap: 30px;
    height: calc(100vh - 240px);
    min-height: 500px;
  }

  /* 左侧目录 */
  .guideline-sidebar {
    width: 300px;
    flex-shrink: 0;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
    overflow: hidden;
  }

  .toc {
    height: 100%;
    overflow-y: auto;
    padding: 10px 0;
  }

  .toc ul {
    list-style: none;
    margin: 0;
    padding: 0;
  }

  .toc-item {
    padding: 12px 20px;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: flex-start;
    color: #333;
    font-size: 14px;
    border-left: 3px solid transparent;
    user-select: none;
    width: 100%;
    box-sizing: border-box;
  }

  .toc-item:hover {
    background-color: rgba(247, 107, 27, 0.05);
    padding-left: 22px;
  }

  .toc-item.active {
    background-color: rgba(247, 107, 27, 0.1);
    border-left-color: #f76b1b;
    color: #f76b1b;
    font-weight: 500;
  }

  .toc-number {
    font-weight: 600;
    margin-right: 8px;
    min-width: 20px;
  }

  .toc-title {
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* 右侧内容区 */
  .guideline-main {
    flex: 1;
    display: flex;
    flex-direction: column;
  }

  .sections-container {
    flex: 1;
    overflow-y: auto;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
    padding: 30px 40px;
    line-height: 1.8;
  }

  .guideline-section {
    margin-bottom: 50px;
    padding-bottom: 30px;
    border-bottom: 1px solid #f0f0f0;
  }

  .guideline-section:last-child {
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 0;
  }

  .section-heading {
    color: #222;
    font-size: 20px;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
  }

  .heading-number {
    color: #f76b1b;
    font-weight: 700;
    margin-right: 10px;
  }

  .section-body {
    color: #444;
    font-size: 15px;
  }

  .section-body ul,
  .section-body ol {
    margin: 20px 0;
    padding-left: 28px;
  }

  .section-body li {
    margin-bottom: 12px;
    padding-left: 5px;
  }

  .section-body li strong {
    color: #f76b1b;
    font-weight: 600;
  }

  .section-body p {
    margin-bottom: 16px;
  }

  .section-body p strong {
    color: #f76b1b;
    font-weight: 600;
  }

  /* 响应式适配 */
  @media (max-width: 1024px) {
    .guideline-content {
      flex-direction: column;
      height: auto;
      gap: 20px;
    }

    .guideline-sidebar {
      width: 100%;
      max-height: 200px;
    }

    .sections-container {
      padding: 20px;
      max-height: 600px;
    }
  }

  @media (max-width: 768px) {
    .guideline-header h1 {
      font-size: 24px;
    }

    .toc-item {
      padding: 10px 15px;
      font-size: 13px;
    }

    .section-heading {
      font-size: 18px;
    }

    .section-body {
      font-size: 14px;
    }
  }
</style>
